package org.yanghaokun.quartz.jobs;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.yanghaokun.order.service.IAdoptOrderService;
import org.yanghaokun.quartz.utils.JobConstants;
import org.yanghaokun.quartz.utils.QuartzJobInfo;

import java.util.Map;

/**
 * 做一个主要的job，后面我们根据type来区分即可
 */
public class MainJob implements Job{

    @Autowired
    private IAdoptOrderService adoptOrderService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //我们从容器种获取存放在里面的参数封装到我们设计好的类里面
        QuartzJobInfo info = (QuartzJobInfo) context.getJobDetail().getJobDataMap().get("params");
        //我们从获取到的这些参数通过type判断是什么类型的订单置向相应的操作
        System.out.println("info" + info);
        byte type = info.getType();
        System.out.println("type" + type);
        switch (type) {
            //如果是领养订单
            case JobConstants.JOB_TYPE_ADOPT_ORDER_CANCEL :
                //通过封装的类获取我们存入里面的参数
                Map<String, Object> params = info.getParams();
                Object orderIdObj = params.get("orderId");
                //做一个非空判断
                if (orderIdObj != null) {
                    //通过订单id查找对应的订单完成取消任务
                    adoptOrderService.cancleById(Long.valueOf(orderIdObj.toString()));
                }
                break;

            case JobConstants.JOB_TYPE_GOODS_ORDER_CANCEL:
                //@TODO
                break;

        }
    }
}
